package com.example.a10928.weifriend.http.configuration;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Date;

import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * Created by Chuang on 2018/2/26.
 * 转换查询参数中的Date为String型的时间秒数
 * <p>
 */

public class DateFieldConverterFactory extends Converter.Factory{
    public static DateFieldConverterFactory create(){
        return new DateFieldConverterFactory();
    }

    @Override
    public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        if (type == Date.class)
            return new DateFieldConverter();
        return null;
    }

    public static class DateFieldConverter implements Converter<Date,String>{

        @Override
        public String convert(Date value) throws IOException {
            return String.valueOf(value.getTime()/1000);
        }
    }
}
